package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import org.cbg.until.TraslationTableZhaohuanshouUntil;
import org.come.Frame.ZhuFrame;
import org.come.entity.Goodstable;
import org.come.until.*;
import org.gemstone.panel.GemMakeTabPanel;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.GemMakeBtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



public class GemMakeJpanel extends JPanel {
    private JScrollPane jScrollPane;
    private JPanel jPanel;
    private List<GemMakeTabPanel> lists;
    private GemMakeBtn equipBtn,// 已装备
            otherBtn, // 其他按钮
            dazaoBtn;// 打造
    private JLabel[] makeLabel;// 背包宝石放置
    private GemMakeBtn[] options;// 其他6个选项

    private GemX[] gemXs;
    private GemX gemX;
    private JLabel essence, essenceLab;// 宝石精华 图片|数量

    public GemMakeJpanel() {
        	// TODO Auto-generated constructor stub
            this.setPreferredSize(new Dimension(552, 375));
            this.setOpaque(false);
            this.setLayout(null);
 
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 84);
            offBtn.setBounds(552 - 37, 10, 25, 25);
            this.add(offBtn);

            options = new GemMakeBtn[6];
            for (int i = 0; i < options.length; i++) {
                String aaa = i == 0 ? "全部" : i == 1 ? "武器" : i == 2 ? "衣服" : i == 3 ? "帽子" : i == 4 ? "项链" : "鞋子";
                options[i] = new GemMakeBtn("skin/8100226/button/49.png", 1, UIUtils.COLOR_BTNTEXT, aaa, UIUtils.TEXT_FONT,
                        2 + i, this);
                options[i].setBounds(147, 48 + i * 17, 68, 17);
                options[i].setVisible(false);
                this.add(options[i]);
            }
            equipBtn = new GemMakeBtn("skin/8100226/newbutton/K60.png", 1, 0, this);
            equipBtn.setBounds(46, 14, 75, 33);
            this.add(equipBtn);
            otherBtn = new GemMakeBtn("skin/8100226/newbutton/K61.png", 1, 1, this);
            otherBtn.setBounds(46+78, 14, 75, 33);
            this.add(otherBtn);
            dazaoBtn = new GemMakeBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, "打造", UIUtils.TEXT_HY16, 8, this);
            dazaoBtn.setBounds(352, 248, 59, 24);
            this.add(dazaoBtn);
            jScrollPane = new JScrollPane();
            jScrollPane.setBounds(55, 60, 182, 295);
            jPanel = TraslationTableZhaohuanshouUntil.TableModel(jScrollPane);
            jScrollPane.getVerticalScrollBar().setUnitIncrement(30);
            this.add(jScrollPane);
            makeLabel = new JLabel[5];
            for (int i = 0; i < makeLabel.length; i++) {
                makeLabel[i] = new JLabel();
                makeLabel[i].setBounds(247 + i * 51, 284, 50, 50);
                makeLabel[i].setOpaque(false);
                MListener ml = new MListener(i);
                makeLabel[i].addMouseListener(ml);
                this.add(makeLabel[i]);
            }
            gemXs = new GemX[3];
            for (int i = 0; i < gemXs.length; i++) {
                JLabel jLabel = new JLabel("", SwingConstants.CENTER);
                jLabel.setFont(UIUtils.TEXT_FONT);
                jLabel.setForeground(Color.white);
                jLabel.setVerticalTextPosition(SwingConstants.CENTER);
                jLabel.setHorizontalTextPosition(SwingConstants.CENTER);
                jLabel.setBounds(331 + i * 57, 69, 47, 47);
                this.add(jLabel);
                JLabel jLabel2 = null;// 额外的i==2时需要
                if (i == 2) {
                    jLabel2 = new JLabel();
                    jLabel2.setIcon(new ImageIcon("inkImg/old/background/8.png"));
                    jLabel2.setBounds(443, 67, 51, 51);
                    this.add(jLabel2);
                }
                GemMakeBtn gemMakeBtn = new GemMakeBtn("skin/8100226/button/34A.png", 1, UIUtils.COLOR_WHITE2, "摘除", UIUtils.TEXT_FONT10, 9 + i, this);
                gemMakeBtn.setBounds(332 + i * 56, 120, 51, 17);
                this.add(gemMakeBtn);
                gemXs[i] = new GemX(jLabel, jLabel2, gemMakeBtn);
            }
            JLabel jLabel = new JLabel();
            jLabel.setBounds(261, 72, 49, 49);
            this.add(jLabel);
            gemX = new GemX(jLabel, null, null);
            essence = new JLabel();
            essence.setBounds(350, 155, 27, 27);
            essence.setIcon(CutButtonImage.getGemstoneIcon("744", 27, 27));
            this.add(essence);
            essenceLab = new JLabel();
            essenceLab.setBounds(398, 164, 80, 14);
            essenceLab.setFont(UIUtils.TEXT_FONT1);
            essenceLab.setForeground(Color.white);
            this.add(essenceLab);
    }

    private ImageIcon icon;
    private ImageIcon iconGun;
    private BigDecimal b = new BigDecimal("5000000");
    // 判断宝石类型
    private int boo = 4;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        	if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B268.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 552, 375, this);
            if (iconGun == null) {
                iconGun = CutButtonImage.getImage("inkImg/old/background/S43.png", -1, -1);
            }
            g.drawImage(iconGun.getImage(), 44, 58, 0, 0, this);
            Util.drawMoney(g, 350, 226);
            Util.drawPrice(g, b, 350, 201);
            GoodsListFromServerUntil.drawGemstoneSuit(g, 5, boo);
    }

    /** 切换 */
    public void qh(final int p) {
    		if (p == 0) {
                try {
                    equipBtn.setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K60.png"));
                    otherBtn.setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K61.png"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jScrollPane.setBounds(45, 70, 180, 272);
                iconGun = CutButtonImage.getImage("inkImg/old/background/S43.png", -1, -1);
                chang(option(-2));
            } else {
                try {
                    equipBtn.setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K59.png"));
                    otherBtn.setIcons(CutButtonImage.cuts("skin/8100226/newbutton/K62.png"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jScrollPane.setBounds(45, 70, 180, 272);
                iconGun = CutButtonImage.getImage("inkImg/old/background/S42.png", -1, -1);
                chang(option(-1));
            }
    }

    /** 界面显示 -2已装备 -1未装备全部 其他是指定部位 */
    public void chang(int type) {
        if (lists == null) {
            lists = new ArrayList<>();
        }
        int size = 0;
        Goodstable[] goods = null;
        if (type == -2) {
            goods = GoodsListFromServerUntil.getChoseGoodsList();
        } else {
            goods = GoodsListFromServerUntil.getGoodslist();
        }
        for (int i = 0; i < goods.length; i++) {
            Goodstable good = goods[i];
            if (good != null) {
                if ((type < 0 && Goodtype.EquipGem(good.getType()))
                        || (type >= 0 && type == Goodtype.EquipmentType(good.getType()))) {
                    if (lists.size() > size) {
                        lists.get(size).SX(good);
                    } else {
                        GemMakeTabPanel gemMakeTabPanel = new GemMakeTabPanel(good);
                        gemMakeTabPanel.addMouseListener(new OptionListener(gemMakeTabPanel));
                        lists.add(gemMakeTabPanel);
                        jPanel.add(gemMakeTabPanel);
                    }
                    size++;
                }
            }
        }
        for (int i = lists.size() - 1; i >= size; i--) {
            jPanel.remove(lists.remove(i));
        }
        tz();
        optionClick(null);
    }

    /** 选项卡处理 -2 -1 */
    public int option(int p) {
        if (p == -2) {
            for (int i = 0; i < options.length; i++) {
                options[i].setVisible(false);
            }
            return -2;
        } else if (p == -1) {
            options[0].setVisible(true);
            for (int i = 1; i < options.length; i++) {
                options[i].setVisible(false);
            }
            return options[0].getB();
        } else if (!options[1].isVisible()) {
            // try {
            // options[0].setIcons(CutButtonImage.cuts("skin/8100226/button/49.png"));
            // } catch (Exception e) {
            // e.printStackTrace();
            // }
            options[0].setText("全部");
            for (int i = 1; i < options.length; i++) {
                options[i].setVisible(true);
            }
            return -2;
        } else {// 重新显示
            for (int i = 1; i < options.length; i++) {
                options[i].setVisible(false);
            }
            int b = p == 2 ? -1 : p == 3 ? 0 : p == 4 ? 3 : p == 5 ? 1 : p == 6 ? 2 : 5;
            String aaa = p == 2 ? "全部" : p == 3 ? "武器" : p == 4 ? "衣服" : p == 5 ? "帽子" : p == 6 ? "项链" : "鞋子";
            try {
                options[0].setText(aaa);
            } catch (Exception e) {
                e.printStackTrace();
            }
            options[0].setB(b);
            return b;
        }
    }

    /** 选项卡物品点击 */
    public void optionClick(Goodstable good) {
        essenceLab.setText(null);
        gemX.SX(good);
        if (good != null) {
            boolean is = false;
            for (int i = lists.size() - 1; i >= 0; i--) {
                GemMakeTabPanel panel = lists.get(i);
                if (panel.getGood().getRgid().compareTo(good.getRgid()) == 0) {
                    panel.setBorder(BorderFactory.createLineBorder(Color.YELLOW));
                    panel.SX(good);
                    is = true;
                } else {
                    panel.setBorder(null);
                }
            }
            if (is) {
                boo = Goodtype.EquipmentType(good.getType());
                for (int i = 0; i < gemXs.length; i++) {
                    gemXs[i].SX(null, 0);
                    if (i == 2) {
                        gemXs[i].setV(boo == 0 ? true : false);
                    }
                }
                String extra = AccessSuitMsgUntil.getExtra(good.getValue(), "宝石属性");
                if (extra != null) {
                    String[] extras = extra.split("&");
                    for (int i = 1; i < extras.length; i++) {
                        BigDecimal id = new BigDecimal(extras[i]);
                        Goodstable goodstable = GoodsListFromServerUntil.fushis.get(id);
                        if (goodstable != null) {
                            int path = Goodtype.GemPath(boo, goodstable.getType());
                            if (path != 0) {
                                path -= 1;
                                gemXs[path].SX(goodstable, 2);
                            }
                        }
                    }
                }
            }
        } else {
            boo = 4;
            for (int i = 0; i < gemXs.length; i++) {
                gemXs[i].SX(null, 0);
                if (i == 2) {
                    gemXs[i].setV(false);
                }
            }
        }

    }

    /** 调整窗体位置 */
    public void tz() {
    		for (int i = 0; i < lists.size(); i++) {
                GemMakeTabPanel modelJpanel = lists.get(i);
                modelJpanel.setBounds(3, i * 69, 160, 67);
            }
            jPanel.setPreferredSize(new Dimension(160, 68 * lists.size()));
            jScrollPane.getViewport().setViewSize(jPanel.getPreferredSize());
    }

    class MListener extends MouseAdapter {
        private int m;

        public MListener(int m) {
            this.m = m;
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Goodstable good = GoodsListFromServerUntil.getGemstoneSuit(m, 0, boo);
            if (good != null) {
                ZhuFrame.getZhuJpanel().creatgoodtext(good);
                MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
            ZhuFrame.getZhuJpanel().cleargoodtext();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            Goodstable good = GoodsListFromServerUntil.getGemstoneSuit(m, 0, boo);
            if (good != null) {
                int path = Goodtype.GemPath(boo, good.getType()) - 1;
                if (gemXs[path].type == 2) {
                    ZhuFrame.getZhuJpanel().addPrompt2("已打造该类型宝石");
                    return;
                }
                for (int i = 0; i < gemXs.length; i++) {
                    if (gemXs[i].type == 1) {
                        gemXs[i].SX(null, 0);
                    }
                }
                gemXs[path].SX(good, 1);
                // 判断等级
                int lvl = Integer.parseInt(good.getValue().split("\\|")[0].split("=")[1]);
                essenceLab.setText((lvl <= 4 ? 1 : lvl - 3) + "/"
                        + GoodsListFromServerUntil.getGoodNum(new BigDecimal(81095)));
                b = new BigDecimal(5000000 + lvl * 1000000);
            }
        }
    }

    class OptionListener extends MouseAdapter {
        private GemMakeTabPanel panel;

        public OptionListener(GemMakeTabPanel panel) {
            this.panel = panel;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            optionClick(panel.getGood());
        }
    }

    class GemX {
        private JLabel jLabel;
        private GemXListener gemXListener;
        private JLabel jLabel2;// 额外的i==2时需要
        private GemMakeBtn gemMakeBtn;
        private int type;

        public GemX(JLabel jLabel, JLabel jLabel2, GemMakeBtn gemMakeBtn) {
            super();
            this.jLabel = jLabel;
            this.gemXListener = new GemXListener();
            this.jLabel.addMouseListener(gemXListener);
            this.jLabel2 = jLabel2;
            this.gemMakeBtn = gemMakeBtn;
        }

        public void SX(Goodstable good) {
            if (good != null) {
                jLabel.setIcon(CutButtonImage.getGemstoneIcon(good.getSkin(), 49, 49));
                gemXListener.rgid = good.getRgid();
                int type = Goodtype.EquipmentType(good.getType());
                for (int i = 0; i < gemXs.length; i++) {
                    if (type == 0) {
                        gemXs[i].jLabel.setText(i == 0 ? "赤炎石" : i == 1 ? "紫烟石" : "孔雀石");
                    } else if (type == 1) {
                        gemXs[i].jLabel.setText(i == 0 ? "落星石" : "芙蓉石");
                    } else if (type == 2) {
                        gemXs[i].jLabel.setText(i == 0 ? "沐阳石" : "芙蓉石");
                    } else if (type == 3) {
                        gemXs[i].jLabel.setText(i == 0 ? "落星石" : "沐阳石");
                    } else if (type == 5) {
                        gemXs[i].jLabel.setText(i == 0 ? "玻璃石" : "寒山石");
                    }
                    gemXs[i].jLabel.setName(gemXs[i].jLabel.getText());
                }
            } else {
                jLabel.setIcon(null);
                gemXListener.rgid = null;
                for (int i = 0; i < gemXs.length; i++) {
                    gemXs[i].jLabel.setText(null);
                    gemXs[i].jLabel.setName(null);
                }
            }
        }

        public void SX(Goodstable good, int type) {
            this.type = type;
            if (good != null) {
                jLabel.setText(null);
                jLabel.setIcon(CutButtonImage.getGemstoneIcon(good.getSkin(), 45, 45));
                gemXListener.rgid = good.getRgid();
            } else {
                jLabel.setText(jLabel.getName());
                jLabel.setIcon(null);
                gemXListener.rgid = null;
            }
            if (type == 0) {
                gemMakeBtn.setIcon(null);
                gemMakeBtn.setText("可打造");
                gemMakeBtn.setForeground(Color.GRAY);
                gemMakeBtn.setBtn(0);
            } else if (type == 1) {
                gemMakeBtn.setIcon(null);
                gemMakeBtn.setText("待打造");
                gemMakeBtn.setForeground(Color.red);
                gemMakeBtn.setBtn(0);
            } else if (type == 2) {
                gemMakeBtn.setText("");
                gemMakeBtn.setBtn(1);
                gemMakeBtn.btnchange(0);
            }
        }

        public void setV(boolean is) {
            jLabel.setVisible(is);
            if (jLabel2 != null) {
                jLabel2.setVisible(is);
            }
            gemMakeBtn.setVisible(is);
        }
    }

    class GemXListener extends MouseAdapter {
        private BigDecimal rgid;

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub
            if (rgid != null) {
                Goodstable goodstable = GoodsListFromServerUntil.getRgid(rgid);
                if (goodstable != null) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
                    ZhuFrame.getZhuJpanel().creatgoodtext(goodstable);
                }
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
            // TODO Auto-generated method stub
            ZhuFrame.getZhuJpanel().cleargoodtext();
        }
    }

    public Goodstable getGood(int type) {
        // 0获取装备//1或者待打造的宝石 //2 3 4拆卸的宝石
        if (type == 0) {
            return GoodsListFromServerUntil.getRgid(gemX.gemXListener.rgid);
        } else if (type == 1) {
            for (int i = 0; i < gemXs.length; i++) {
                if (gemXs[i].type == 1) {
                    return GoodsListFromServerUntil.getRgid(gemXs[i].gemXListener.rgid);
                }
            }
        } else {
            GemX gemX = gemXs[type - 2];
            if (gemX.type == 2) {
                return GoodsListFromServerUntil.getRgid(gemX.gemXListener.rgid);
            }
        }
        return null;
    }

}
